﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using Microsoft.ApplicationBlocks.Data;
using MCMLXVII.BackEnd.Data.OrganizationChart.DataSets;
using System.ServiceModel;
using MCMLXVII.BackEnd.Services.Common.Messages;

#endregion

namespace MCMLXVII.BackEnd.Data.OrganizationChart
{
    public class TreeAndNodes
    {
        public TreeAndNodes()
        {

        }

        #region Get Functions (Read)
        public static DSorganizationTreeNode GetTree(int MemberID)
        {
            DSorganizationTreeNode ds = new DSorganizationTreeNode();
            ds.OrganizationTree.Load(SqlHelper.ExecuteReader("GetOrganizationTree", new object[] { MemberID }));
            ds.OrganizationTreeXLated.Load(SqlHelper.ExecuteReader(System.Data.CommandType.Text, "select NodeID, Language, Name from OrganizationTreeXLated order by NodeID, Language"));
            if (ds.OrganizationTree.Count > 0)
                return ds;
            else
                return null;
        }

        public static DSorganizationTreeNode.OrganizationTreeDataTable GetTreeFromNode(int NodeID, int MemberID)
        {
            DSorganizationTreeNode.OrganizationTreeDataTable  dt = new DSorganizationTreeNode.OrganizationTreeDataTable();
            dt.Load(SqlHelper.ExecuteReader("GetOrganizationTreeFromNode", new object[] { NodeID, MemberID }));
            if (dt.Count > 0)
                return dt;
            else
                return null;
        }

        public static DSorganizationTreeNode GetOrganizationTreeNodeInfo(int NodeID, int MemberID)
        {
            DSorganizationTreeNode ds = new DSorganizationTreeNode();
            ds.OrganizationTree.Load(SqlHelper.ExecuteReader("GetOrganizationTreeNodeInfo", new object[] { NodeID, MemberID }));
            ds.OrganizationTreeXLated.Load(SqlHelper.ExecuteReader(System.Data.CommandType.Text, "select NodeID, Language, Name from OrganizationTreeXLated where NodeID = " + NodeID.ToString()  +" order by NodeID, Language"));
            if (ds.OrganizationTree.Count > 0)
                return ds;
            else
                return null;
        }

        
        public static DSorganizationTreeNode GetDestinationTree(int MemberID, int ExcludeNodeID )
        {
            DSorganizationTreeNode ds = new DSorganizationTreeNode();
            ds.OrganizationTree.Load(SqlHelper.ExecuteReader("GetDestinationOrganizationTree", new object[] { MemberID, ExcludeNodeID  }));
            if (ds.OrganizationTree.Count > 0)
                return ds;
            else
                return null;
        }



        public static string[] GetOrganizationTreeNodeEfectiveRights(int NodeID, int MemberID)
        {
            DbDataReader sr = SqlHelper.ExecuteReader("GetOrganizationTreeNodeEfectiveRights", new object[] { NodeID, MemberID });
            List<string> ls = new List<string>();
            while (sr.Read())
            {
                ls.Add(sr.GetString(0));
            }
            return ls.ToArray();
        }



        public static DSOrganizationAudit.TreeNodeAuditDataTable  GetOrganizationTreeNodeAudit(int NodeID)
        {
            DSOrganizationAudit.TreeNodeAuditDataTable dt = new DSOrganizationAudit.TreeNodeAuditDataTable();
            dt.Load(SqlHelper.ExecuteReader("dbo.GetOrganizationTreeNodeAudit", new object[] { NodeID }));
            if (dt.Rows.Count > 0)
                return dt;
            else
                return null;
        }

        public static DSOrganizationTreeNodeSecurity GetOrganizationTreeNodeSecurity(int NodeID)
        {
            DSOrganizationTreeNodeSecurity ds = new DSOrganizationTreeNodeSecurity();            
            ds.OrganizationTreeNodeDefinedRights.Load (SqlHelper.ExecuteReader("dbo.GetOrganizationTreeNodeDefinedRights", NodeID));
            ds.OrganizationTreeNodeInheritedRights.Load(SqlHelper.ExecuteReader("dbo.GetorganizationTreeNodeInheritedRights", NodeID));
            return ds;
        }

        #endregion
        #region Create, Update & Delete Nodes

        public static DSOrganizationTreeNewItem.NewCreatedItemRow CreateNewOrganizationTreeNode(int FatherNodeID, int MemberID, string NodeName)
        {
            DSOrganizationTreeNewItem.NewCreatedItemDataTable dt = new DSOrganizationTreeNewItem.NewCreatedItemDataTable();
            dt.Load(SqlHelper.ExecuteReader("CreateNewOrganizationTreeNode", MemberID, FatherNodeID, NodeName));
            if (dt.Rows.Count > 0)
                return (DSOrganizationTreeNewItem.NewCreatedItemRow)dt.Rows[0];
            else
                return null;
        }

        public static int UpdateOrganizationTreeNode(int NodeID, string NodeName, int FatherNodeID, string DirectoryServiceConfigInfo, int UpdateSequence, string Translations, int PerformerMemberID )
        {
            try
            {
                return (int)SqlHelper.ExecuteScalar("UpdateOrganizationTreeNodeCore", NodeID, NodeName, FatherNodeID, DirectoryServiceConfigInfo, UpdateSequence, Translations, PerformerMemberID );
            }
            catch(DbException ex)
            {
                if (ex.ErrorCode ==500001)
                    {
                        throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.DataUpdateSequence));
                    }
                else
                    {
                        throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Technical, ex.Message));
                    }                
             }           
        }

        public static int MoveOrganizationTreeNode(int NodeID, int DestinationNodeID, int UpdateSequence, int PerformerMemberID)
        {
            try
            {
                return (int)SqlHelper.ExecuteScalar("MoveOrganizationTreeNode", NodeID, DestinationNodeID , UpdateSequence, PerformerMemberID);
            }
            catch (DbException ex)
            {
                if (ex.ErrorCode ==500001)
                    {
                        throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.DataUpdateSequence));
                    }
                else
                    {
                        throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Technical, ex.Message));
                    }                
            }
        }


        public static void DeleteOrganizationTreeNode(int NodeID, int PerformerMemberID)
        { 
        
            try
            {
                SqlHelper.ExecuteNonQuery("DeleteOrganizationTreeNode", NodeID, PerformerMemberID );
            }
            catch (DbException ex)
            {
                switch (ex.ErrorCode )
                {
                    case 50002:
                        throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Security, Properties.Resources.NodeHasChildNodes));

                    case 50003: 
                        throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Security, Properties.Resources.NodehasMembers ));
                    case 50004:
                        throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Security, Properties.Resources.NodeIsRootNode ));
                }
                                        throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Technical, ex.Message ));
            }        
        }

        public static int UpdateOrganizationTreeNodeSecurity(int NodeID, int MemberID, DSOrganizationTreeNodeSecurity.OrganizationTreeNodeDefinedRightsDataTable permissions, int UpdateSequence, bool BlockInheritance)
        {

            //DbTransaction tran = SqlHelper.CreateTransaction();
            string _BlockInheritance;
            if (BlockInheritance)
                _BlockInheritance = "Y";
            else
                _BlockInheritance = "N";
            int NewUpdateSequence = 0;
            try
            {

                NewUpdateSequence = (int)SqlHelper.ExecuteScalar("dbo.UpdateorganizationTreeNodeSecurity_A", NodeID, MemberID, UpdateSequence, _BlockInheritance  );
                foreach (DSOrganizationTreeNodeSecurity.OrganizationTreeNodeDefinedRightsRow row in permissions)
                {
                    SqlHelper.ExecuteNonQuery("dbo.UpdateorganizationTreeNodeSecurity_B", NodeID, row.MemberID, row.Permission);
                }
                //tran.Commit();
            }
            catch (DbException ex)
            {
                //tran.Rollback();
                if (ex.ErrorCode == 500001)
                {
                    throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.DataUpdateSequence ));
                }
                else
                {
                    throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Technical, ex.Message ));
                }
            }
            return NewUpdateSequence;
        }
        #endregion

        public static void UpdateOrganizationTreeNodeProperty(int NodeID, string PropertyName, string PropertyValue)
        {
            //SqlHelper.ExecuteNonQuery("dbo.UpdateOrganizationTreeNodeProperty", NodeID, PropertyName, PropertyValue);
        }

    }
}
